package com.team.book.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.team.book.common.constant.BooleanEnum;
import com.team.book.common.constant.ResponseStatus;
import com.team.book.common.domain.Page;
import com.team.book.common.exception.UncheckedException;
import com.team.book.common.util.DateUtil;
import com.team.book.common.util.UserUtil;
import com.team.book.domain.Book;
import com.team.book.domain.BookClassification;
import com.team.book.domain.BookExample;
import com.team.book.domain.User;
import com.team.book.mapper.BookClassificationMapper;
import com.team.book.mapper.BookMapper;
import com.team.book.service.BookService;
import com.team.book.service.BorrowedRecordService;
import com.team.book.web.dto.BookDTO;
import com.team.book.web.dto.BookStatisticsDTO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 
 * @author Joann
 * @date 2017年5月6日 下午11:38:55
 */
@Service
public class BookServiceImpl implements BookService {
	@Autowired
	public BookMapper bookMapper;
	@Autowired
	public BorrowedRecordService borrowedRecordService;
	@Autowired
	public BookClassificationMapper classificationMapper;

	/**
	 * 书籍分页查询
	 * @param page
	 * @param book
     * @return
     */
	@Override
	public Page<BookDTO> query(Page<BookDTO> page, BookDTO book) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("book",book);
		long count = bookMapper.count(map);
		map.put("limit",page.getPageSize());
		map.put("offset",page.getOffset());
		page.setTotalCount(count);
		List<BookDTO> list = bookMapper.query(map);
		page.setResult(list);
		return page;
	}

	/**
	 * 通过图书编号获取
	 * @param code
	 * @return
     */
	@Override
	public Book get(Long code) throws UncheckedException {
		if(StringUtils.isEmpty(code)){
			throw  new UncheckedException(ResponseStatus.NOT_FOUND);
		}
		BookExample book = new BookExample();
		BookExample.Criteria cr = book.createCriteria();
		cr.andStatusEqualTo(BooleanEnum.TRUE.getCode());
		cr.andCodeEqualTo(code);
		List<Book> list = bookMapper.selectByExample(book);
		if(list!=null&&list.size()>0){
			return list.get(0);
		}else{
			throw  new UncheckedException(ResponseStatus.NOT_FOUND);
		}
	}

	/**
	 * 添加图书
	 * @param book
	 * @return
     */
	@Override
	public int save(Book book) {
		User user = UserUtil.getCurUser();
		book.setCreateuser(user.getId());
		book.setCreatetime(DateUtil.curDate());
		book.setCode(new Date().getTime());
		book.setStatus(BooleanEnum.TRUE.getCode());
		book.setBorrowStatus(BooleanEnum.TRUE.getCode());
		return bookMapper.insert(book);
	}

	/**
	 * 更新图书
	 * @param book
	 * @return
     */
	@Override
	public int update(Book book) throws UncheckedException {
		Book original = this.get(book.getCode());
		original.setAuthor(book.getAuthor());
		original.setBlurb(book.getBlurb());
		original.setClassification(book.getClassification());
		original.setTitle(book.getTitle());
		original.setType(book.getType());
		original.setUpdatetime(DateUtil.curDate());
		User user = UserUtil.getCurUser();
		original.setUpdateuser(user.getId());
		return bookMapper.updateByPrimaryKey(original);
	}

	/**
	 * 删除图书
	 * @param code
	 * @return
     */
	@Override
	public int del(Long code) throws UncheckedException {
		Book book = this.get(code);
		book.setStatus(BooleanEnum.FALSE.getCode());
		User user = UserUtil.getCurUser();
		book.setUpdateuser(user.getId());
		return bookMapper.updateByPrimaryKey(book);
	}

	/**
	 * 借书
	 * @param code
	 * @return
     */
	public int borrow(Long code) throws UncheckedException {
		Book book = this.get(code);
		if(BooleanEnum.FALSE.getCode()==book.getBorrowStatus()){
			throw  new UncheckedException(ResponseStatus.NOT_BORROW);
		}
		book.setBorrowStatus(BooleanEnum.FALSE.getCode());
		User user = UserUtil.getCurUser();
		book.setUpdateuser(user.getId());
		book.setUpdatetime(DateUtil.curDate());
		borrowedRecordService.save(code);
		return bookMapper.updateByPrimaryKey(book);
	}

	/**
	 * 还书
	 * @param code
	 * @return
	 */
	public int returnBook(Long code) throws UncheckedException {
		Book book = this.get(code);
		if(BooleanEnum.FALSE.getCode()!=book.getBorrowStatus()){
			throw  new UncheckedException(ResponseStatus.NOT_RETURN_BOOK);
		}
		book.setBorrowStatus(BooleanEnum.TRUE.getCode());
		User user = UserUtil.getCurUser();
		book.setUpdateuser(user.getId());
		book.setUpdatetime(DateUtil.curDate());
		borrowedRecordService.returnBook(code);
		return bookMapper.updateByPrimaryKey(book);
	}

	/**
	 * 添加图书分类
	 * @param classification
	 * @return
     */
	@Override
	public int saveClassification(BookClassification classification) {
		classification.setCreatetime(DateUtil.curDate());
		classification.setStatus(BooleanEnum.TRUE.getCode());
		return classificationMapper.insert(classification);
	}

	/**
	 * 修改分类
	 * @param classification
	 * @return
	 */
	@Override
	public int updateClassification(BookClassification classification) throws UncheckedException {
		BookClassification bc = classificationMapper.selectByPrimaryKey(classification.getId());
		if(bc==null){
			throw new UncheckedException(ResponseStatus.NOT_FOUND);
		}
		bc.setClassificationName(classification.getClassificationName());
		return classificationMapper.updateByPrimaryKey(classification);
	}

	/**
	 * 删除分类
	 * @param classification
	 * @return
	 */
	@Override
	public int delClassification(BookClassification classification) throws UncheckedException {
		BookClassification bc = classificationMapper.selectByPrimaryKey(classification.getId());
		if(bc==null){
			throw new UncheckedException(ResponseStatus.NOT_FOUND);
		}
		bc.setStatus(BooleanEnum.FALSE.getCode());
		return classificationMapper.updateByPrimaryKey(bc);
	}


	/**
	 * 查询图书分类
	 * @return
     */
	@Override
	public List<BookClassification> queryClassification() {
		return classificationMapper.query();
	}

	/**
	 * 书籍统计查询
	 * @param page
	 * @return
	 */
	@Override
	public Page<BookStatisticsDTO> queryClassificationStatistics(Page<BookStatisticsDTO> page){
		Map<String,Object> map = new HashMap<>();
		long count = bookMapper.countClassificationStatistics(map);
		map.put("limit",page.getPageSize());
		map.put("offset",page.getOffset());
		page.setTotalCount(count);
		List<BookStatisticsDTO> list = bookMapper.queryClassificationStatistics(map);
		page.setResult(list);
		return page;
	}

	/**
	 * 书籍统计查询
	 * @param page
	 * @return
	 */
	@Override
	public Page<BookStatisticsDTO> queryBorrowStatistics(Page<BookStatisticsDTO> page){
		Map<String,Object> map = new HashMap<>();
		long count = bookMapper.countClassificationStatistics(map);
		map.put("limit",page.getPageSize());
		map.put("offset",page.getOffset());
		page.setTotalCount(count);
		List<BookStatisticsDTO> list = bookMapper.queryBorrowStatistics(map);
		page.setResult(list);
		return page;
	}
}
